package com.my.sys.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.my.app.http.PageResult;
import com.my.sys.dao.SysUserDao;
import com.my.sys.model.SysPermission;
import com.my.sys.model.SysRole;
import com.my.sys.model.SysUser;
import com.my.sys.repository.SysUserRepository;
import com.my.redis.dao.IUserManager;
import com.my.sys.service.SysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.credential.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class SysUserServiceImpl implements SysUserService {

    private final Logger logger = LogManager.getLogger(getClass());

    @Autowired(required = false)
    private PasswordService passwordService;
    @Autowired(required = false)
    private IUserManager userDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRepository sysUserRepository;

    public int login(String username, String password) {
        logger.info("Username[{}] password[{}]", username, password);
        SysUser user = sysUserRepository.findByUsername(username);
        if (user != null) {
            if (passwordService.passwordsMatch(password, user.getPassword())) {
                return 1;
            }
            return -1;
        }
        return 0;
    }

    public SysUser saveOrUpdate(SysUser record) {
        logger.info("user save or update ...");

        if (record.getId() != null) {
            SysUser user = sysUserRepository.findOne(record.getId());
            if (user != null) {
                record.setId(user.getId());
                record.setUsername(user.getUsername());
                record.setPassword(user.getPassword());
            }
        }else {
            if (!StringUtils.isEmpty(record.getPassword())) {
                record.setPassword(passwordService.encryptPassword(record.getPassword()));
            }
        }
        return sysUserRepository.save(record);
    }

    public List<SysUser> saveOrUpdate(List<SysUser> records) {
        return null;
    }

    public void delete(Long aLong) {

    }

    public void delete(SysUser record) {

    }

    public SysUser findByUsername(String username) {
        SysUser record = sysUserRepository.findByUsername(username);
        Set<SysRole> roles = record.getRoles();
        logger.info("{} user has roles size: {}", username, roles.size());
        for (SysRole role:roles) {
            Set<SysPermission> permissions = role.getPermissions();
            logger.info("{} role has Permissions size: {}", role.getName(), permissions.size());
            role.setPermissions(permissions);
        }
        record.setRoles(roles);
        return record;
    }

    public SysUser find(Long id) {
        return sysUserDao.findById(id);
    }

    public List<SysUser> find(SysUser record) {
        return null;
    }

    public List<SysUser> findAll() {
        return sysUserRepository.findAll();
    }

    public PageResult<SysUser> findPage(SysUser record, int pageNum, int pageSize) {
        return null;
    }

}
